package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.mapper.TbItemParamItemMapper;
import com.usian.mapper.TbItemParamMapper;
import com.usian.pojo.TbItemParam;
import com.usian.pojo.TbItemParamItem;
import com.usian.redis.RedisClient;
import com.usian.service.ItemParamService;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@Service
@Transactional
public class ItemParamServiceImpl implements ItemParamService {
    @Resource
    TbItemParamMapper tbItemParamMapper;
    @Resource
    TbItemParamItemMapper tbItemParamItemMapper;
    @Value("${ITEM_INFO}")
    private String ITEM_INFO;

    @Value("${PARAM}")
    private String PARAM;

    @Value("${ITEM_INFO_EXPIRE}")
    private Integer ITEM_INFO_EXPIRE;

    @Value("${SETNX_PARAM_LOCK_KEY}")
    private String SETNX_PARAM_LOCK_KEY;
    @Resource
    private RedisClient redisClient;
    //根据商品分类 ID 查询规格参数模板
    @Override
    public TbItemParam selectItemParamByItemCatId(Long itemCatId) {
        QueryWrapper<TbItemParam> tbItemParamQueryWrapper = new QueryWrapper<>();
        tbItemParamQueryWrapper.eq("item_cat_id",itemCatId);
        List<TbItemParam> list = tbItemParamMapper.selectList(tbItemParamQueryWrapper);
        /*for (int i = 0; i < tbItemParams.size(); i++) {
            *//*TbItem tbItemParam =  tbItemParams.get(i);
            tbItem.setPrice(tbItem.getPrice()/100);*//*

        }*/
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
    //查询商品规格列表
    @Override
    public PageResult selectItemParamAll(Integer page, Integer rows) {
        PageHelper.startPage (page,rows);
        QueryWrapper<TbItemParam> tbItemParamQueryWrapper = new QueryWrapper<>();
        tbItemParamQueryWrapper.orderByDesc("updated");
        List<TbItemParam> tbItemParams = tbItemParamMapper.selectList(tbItemParamQueryWrapper);
        PageInfo<TbItemParam> pageInfo = new PageInfo<>(tbItemParams);
        PageResult pageResult = new PageResult();
        pageResult.setPageIndex(page);
        pageResult.setResult(pageInfo.getList());
        pageResult.setTotalPage(Long.valueOf(pageInfo.getPages()));
        return pageResult;
    }
    //添加商品规格列表
    @Override
    public Integer insertItemParam(Long itemCatId, String paramData) {
        //判断这个商品是否已经有规格
        QueryWrapper<TbItemParam> tbItemParamQueryWrapper = new QueryWrapper<>();
        tbItemParamQueryWrapper.eq("item_cat_id",itemCatId);
        List<TbItemParam> tbItemParams = tbItemParamMapper.selectList(tbItemParamQueryWrapper);
        if (tbItemParams.size()>0){
            return 0;
        }
        //添加规格
        //保存规格模板
        Date date = new Date();
        TbItemParam tbItemParam = new TbItemParam();
        tbItemParam.setItemCatId(itemCatId);
        tbItemParam.setParamData(paramData);
        tbItemParam.setUpdated(date);
        tbItemParam.setCreated(date);
        return tbItemParamMapper.insertSelective(tbItemParam);

    }
    //删除规格模板
    @Override
    public Integer deleteItemParamById(Integer id) {
        return tbItemParamMapper.deleteByPrimaryKey((long)id);
    }
    //根据商品 ID 查询商品规格参数
    @Override
    public TbItemParamItem selectTbItemParamItemByItemId(Long itemId) {
        //查询缓存
        TbItemParamItem tbItemParamItem = (TbItemParamItem) redisClient.get(
                ITEM_INFO + ":" + itemId + ":"+ PARAM);
        if(tbItemParamItem!=null){
            return tbItemParamItem;
        }
        if (redisClient.setnx(SETNX_PARAM_LOCK_KEY+":"+itemId,itemId,5L)) {
            QueryWrapper<TbItemParamItem> tbItemParamItemQueryWrapper = new QueryWrapper<>();
            tbItemParamItemQueryWrapper.eq("item_id", itemId);
            List<TbItemParamItem> tbItemParamItemList =
                    tbItemParamItemMapper.selectList(tbItemParamItemQueryWrapper);
            if (tbItemParamItemList != null && tbItemParamItemList.size() > 0) {
                //把数据保存到缓存
                redisClient.set(ITEM_INFO + ":" + itemId + ":" +
                        PARAM, tbItemParamItemList.get(0));
                //设置缓存的有效期
                redisClient.expire(ITEM_INFO + ":" + itemId + ":" + PARAM, ITEM_INFO_EXPIRE);

            }
            else {
                //********************解决缓存穿透************************/
                //如果redis和数据库都没有数据在redis里存null并设置30秒直接返回null
                //把空对象保存到缓存

                redisClient.set(ITEM_INFO + ":" + itemId + ":" + PARAM, new TbItemParamItem());
                //设置缓存的有效期
                redisClient.expire(ITEM_INFO + ":" + itemId + ":" + PARAM, 30);
            }
            redisClient.del(SETNX_PARAM_LOCK_KEY+":"+itemId);
            if (tbItemParamItemList != null && tbItemParamItemList.size() > 0){
                return tbItemParamItemList.get(0);
            }
            return new TbItemParamItem();
        }else {

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return selectTbItemParamItemByItemId(itemId);
        }
    }
}
